﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMovies
{
    enum Types { Duration, Language, BitRate, SamplingRate }
    class Decoder
    {
        public static string Decode<T>(T value, Types type)
        {
            switch (type)
            {
                case Types.Duration:
                    int total = (int)Convert.ChangeType(value, typeof(int));
                    int min = total / 60000;
                    int ore = min / 60;
                    min = min - ore * 60;
                    int sec = (total / 1000 - (ore * 60 + min) * 60);
                    return ore > 0
                            ? string.Format("{0}h {1}' {2}\"", ore, min, sec)
                            : string.Format("{0}' {1}\"", min, sec);
                case Types.Language:
                    string lng = value.ToString();
                    switch (lng)
                    {
                        case "en":
                            return Properties.Resources.English;
                        case "it":
                            return Properties.Resources.Italian;
                        case "de":
                            return Properties.Resources.German;
                        case "fr":
                            return Properties.Resources.Francaise;
                        default:
                            return lng;
                    }
                case Types.BitRate:
                    string[] brate = value.ToString().Split('/');
                    string result = "";
                    foreach (string br in brate)
                    {
                        int val = 0;
                        if (int.TryParse(br, out val))
                        {
                            result = result == ""
                                        ? string.Format("{0} Kbs", val / 1000)
                                        : string.Format("{0} / {0} Kbs", result, val / 1000);
                        }
                    }
                    return result;
                case Types.SamplingRate:
                    string[] srate = value.ToString().Split('/');
                    string aresult = "";
                    foreach (string br in srate)
                    {
                        int val = 0;
                        if (int.TryParse(br, out val))
                        {
                            aresult = aresult == ""
                                        ? string.Format("{0} KHz", val / 1000)
                                        : string.Format("{0} / {0} KHz", aresult, val / 1000);
                        }
                    }
                    return aresult;
            }
            return "";
        }
    }
}
